﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RecycleResetSelectPage : Page
    {
        public UITriggerTab heroTab;
        public UITriggerTab equipTab;
        public UITriggerTab treaTab;

        public RecycleResetSelectHeroPanel heroPanel;
        public RecycleResetSelectEquipPanel equipPanel;
        public RecycleResetSelectTreaPanel treaPanel;

        public List<UITweener> tweeners;

        public bool isPlaying = false;
        int tabIndex = 0;

        protected override void DoOpen()
        {
            if (options.ContainsKey("type"))
            {
                if (options["type"] == "0")
                {
                    tabIndex = 0;
                }
                else if (options["type"] == "1")
                {
                    tabIndex = 1;
                }
                else if (options["type"] == "2")
                {
                    tabIndex = 2;
                }
            }
            else
            {
                tabIndex = 0;
            }
            OpenSubContent();
            foreach (var tweener in tweeners)
            {
                if (tweener != null)
                {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                }
            }
        }

        protected override void DoClose()
        {
            heroPanel.OnPageClose();
            equipPanel.OnPageClose();
            treaPanel.OnPageClose();

            StaticData.curRecycleResetSelectType = tabIndex;
        }

        public override void OnCoverPageRemove()
        {
            heroPanel.OnRemoveCoverPage();
            equipPanel.OnRemoveCoverPage();
            treaPanel.OnRemoveCoverPage();
        }

        public void OnHeroTabClick()
        {
            if (tabIndex == 0)
            {
                return;
            }
            StaticData.ClearRecycleResetSelect();
            tabIndex = 0;
            OpenSubContent();
        }

        public void OnEquipTabClick()
        {
            if (tabIndex == 1)
            {
                return;
            }
            StaticData.ClearRecycleResetSelect();
            tabIndex = 1;
            OpenSubContent();
        }

        public void OnTreaTabClick()
        {
            if (tabIndex == 2)
            {
                return;
            }
            StaticData.ClearRecycleResetSelect();
            tabIndex = 2;
            OpenSubContent();
        }

        void OpenSubContent()
        {
            List<UITweener> content_tweeners = new List<UITweener>();
            switch (tabIndex)
            {
                case 0:
                    heroTab.Trigger();
                    equipTab.UnTrigger();
                    treaTab.UnTrigger();
                    heroPanel.ShowTab();
                    equipPanel.CloseTab();
                    treaPanel.CloseTab();
                    content_tweeners = heroPanel.tweeners;
                    break;
                case 1:
                    heroTab.UnTrigger();
                    equipTab.Trigger();
                    treaTab.UnTrigger();
                    heroPanel.CloseTab();
                    equipPanel.ShowTab();
                    treaPanel.CloseTab();
                    content_tweeners = equipPanel.tweeners;
                    break;
                case 2:
                    heroTab.UnTrigger();
                    equipTab.UnTrigger();
                    treaTab.Trigger();
                    heroPanel.CloseTab();
                    equipPanel.CloseTab();
                    treaPanel.ShowTab();
                    content_tweeners = treaPanel.tweeners;
                    break;

            }
            float tweenerTime = 0;
            foreach (var tweener in content_tweeners)
            {
                tweener.ResetToBeginning();
                tweener.PlayForward();
                if (tweenerTime < tweener.delay + tweener.duration)
                {
                    tweenerTime = tweener.delay + tweener.duration;
                }
            }
            isPlaying = true;
            Invoke("OnOpenSubPageTweenOver", tweenerTime);
        }

        void OnOpenSubPageTweenOver()
        {
            switch (tabIndex)
            {
                case 0:
                    heroPanel.OnOpenSubPageTweenOver();
                    break;
                case 1:
                    equipPanel.OnOpenSubPageTweenOver();
                    break;
                case 2:
                    treaPanel.OnOpenSubPageTweenOver();
                    break;
            }
            isPlaying = false;
        }

    }
}
